package com.example.administrator.myproject.merchant

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.example.administrator.myproject.R
import com.example.administrator.myproject.bean.BobmBean



import java.io.IOException
import java.io.InputStream
import java.net.URL
import java.net.URLConnection

class HomeAdapter(private val mContext: Context) : RecyclerView.Adapter<HomeAdapter.Holder>() {
    private val inflater: LayoutInflater
    private var list: List<BobmBean>? = null

    init {
        this.inflater = LayoutInflater.from(mContext)
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): Holder {
        val view = inflater.inflate(R.layout.home_item, viewGroup, false)
        return Holder(view)
    }

    override fun onBindViewHolder(holder: Holder, i: Int) {
        holder.money.text = "￥" + list!![i].money.toString()
        holder.type.text = list!![i].type
        holder.name.text = list!![i].name
        if (list!![i].name!!.equals("拌饭")) {
            holder.imageView.setImageDrawable(mContext.resources.getDrawable(R.drawable.laodiexie))
        } else if (list!![i].name!!.equals("米饭")) {
            holder.imageView.setImageDrawable(mContext.resources.getDrawable(R.drawable.refeng))
        } else if (list!![i].name!!.equals("米线")) {
            holder.imageView.setImageDrawable(mContext.resources.getDrawable(R.drawable.nanxie))
        } else if (list!![i].name!!.equals("面")) {
            holder.imageView.setImageDrawable(mContext.resources.getDrawable(R.drawable.kuzi_9_fen))
        } else {
            holder.imageView.setImageDrawable(mContext.resources.getDrawable(R.drawable.mian_fan))
        }
        holder.itemView.setOnClickListener {
            val intent = Intent()
            intent.setClass(mContext, Details::class.java)
            intent.putExtra("objectId", list!![i].objectId)
            mContext.startActivity(intent)
        }
    }

    override fun getItemCount(): Int {
        return if (list == null) 0 else list!!.size
    }

    fun setData(li: List<BobmBean>) {
        this.list = li
        notifyDataSetChanged()
    }

    inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val name: TextView
        val type: TextView
        val money: TextView
        val imageView: ImageView

        init {
            imageView = itemView.findViewById(R.id.home_iv)
            name = itemView.findViewById(R.id.home_name)
            type = itemView.findViewById(R.id.home_type)
            money = itemView.findViewById(R.id.home_money)
        }
    }
}
